<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>编辑游戏信息</title>
    <link href="../common/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            background-color: #f8f9fa;
        }

        .container {
            margin-top: 50px;
        }

        h2 {
            color: #007bff;
        }

        form {
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        label {
            font-weight: bold;
        }

        textarea {
            resize: vertical;
        }

        img {
            margin-top: 10px;
            display: block;
            max-width: 100px;
            max-height: 100px;
        }

        button {
            margin-top: 20px;
        }
    </style>
</head>

<body>
    <?php
    include './header.php';
    include_once '../conn.php';

    // 获取要编辑的游戏 ID
    $gameId = $_GET["id"];

    // 查询游戏信息
    $queryGame = "SELECT * FROM games WHERE game_id = $gameId";
    $resultGame = $conn->query($queryGame);

    if ($resultGame->num_rows > 0) {
        $rowGame = $resultGame->fetch_assoc();
    } else {
        echo "<div class='container'>";
        echo "<div class='alert alert-danger' role='alert'>游戏不存在</div>";
        echo "</div>";
        exit(); // 游戏不存在，结束页面显示
    }
    ?>

    <div class="container">
        <h2>编辑游戏信息</h2>

        <!-- 游戏编辑表单 -->
        <form action="update_game.php" method="post" enctype="multipart/form-data">
            <input type="hidden" name="game_id" value="<?php echo $rowGame['game_id']; ?>">

            <div class="mb-3">
                <label for="title" class="form-label">游戏名称</label>
                <input type="text" class="form-control" id="title" name="title" value="<?php echo $rowGame['title']; ?>" required>
            </div>

            <div class="mb-3">
                <label for="description" class="form-label">游戏介绍</label>
                <textarea class="form-control" id="description" name="description" rows="3"><?php echo $rowGame['description']; ?></textarea>
            </div>

            <div class="mb-3">
                <label for="publisher" class="form-label">出版公司</label>
                <input type="text" class="form-control" id="publisher" name="publisher" value="<?php echo $rowGame['publisher']; ?>">
            </div>

            <div class="mb-3">
                <label for="release_date" class="form-label">发行时间</label>
                <input type="date" class="form-control" id="release_date" name="release_date" value="<?php echo $rowGame['release_date']; ?>">
            </div>

            <div class="mb-3">
                <label for="rating" class="form-label">评分</label>
                <input type="number" class="form-control" id="rating" name="rating" step="0.1" min="0" max="10" value="<?php echo $rowGame['rating']; ?>">
            </div>
 
            <div class="mb-3">
                <label for="category" class="form-label">游戏类别</label>
                <select class="form-select" id="category" name="category">
                    <?php
                    // 查询游戏类别
                    $queryCategories = "SELECT * FROM categories";
                    $resultCategories = $conn->query($queryCategories);

                    while ($rowCategory = $resultCategories->fetch_assoc()) {
                        $selected = ($rowCategory['category_id'] == $rowGame['category_id']) ? "selected" : "";
                        echo "<option value='{$rowCategory['category_id']}' $selected>{$rowCategory['name']}</option>";
                    }
                    ?>
                </select>
            </div>

            <div class="mb-3">
                <label for="image" class="form-label">游戏图片</label>
                <input type="file" class="form-control" id="image" name="image">
                <img src="../<?php echo $rowGame['image_path']; ?>" alt="Game Image">
            </div>

            <button type="submit" class="btn btn-primary">保存修改</button>
        </form>
    </div>
</body>

</html>
